/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class DataPairFieldTime extends DataPairDefault implements FieldBinding, ActionListener {
    private BindTimeInterface field;
    private JFormattedTextField text_field = null;
    private SimpleDateFormat time_format = new SimpleDateFormat(Settings.getString("gui.gui.db_tools.time_format", "HH:mm:ss"));
    //private SimpleDateFormat time_format = null;
    
    public DataPairFieldTime(Binder binder,BindTimeInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldTime(Binder binder,BindTimeInterface p_field, boolean editable) {
        super(binder,p_field);
        time_format = p_field.getTimeFormat();
        //System.out.println("TimeFormat = " + p_field.getTimeFormat() + " " + p_field.getTimeFormatDescription());
        text_field = new JFormattedTextField(time_format);
        text_field.setColumns(7);
        
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        Box box = Box.createHorizontalBox();
        box.add(text_field);
        box.add(Box.createHorizontalStrut(10));
        //box.add(new JLabel(Settings.getString("gui.gui.db_tools.time_format", "HH:mm:ss")));
        box.add(new JLabel(p_field.getTimeFormatDescription()));
        //box.add(new JLabel(p_field.getTimeFormatDescription()));
        setComponent(box);
        text_field.setEditable(editable);
    }
    
    public boolean isChanged() {
        Date value = field.get();
        if (value == null) {
            return text_field.getText() != null;
        } else {
            try {
                return !value.equals(time_format.parse(text_field.getText()));
            } catch (ParseException pe) {
                return false;
            }
        }
    }
    
    public void intoControl() {
        Date date = field.get();
        if (date != null) {
            text_field.setValue(date);
        } else {
            text_field.setValue(null);
        }
    }
    
    public void intoBindable() {
        try {
            String date = text_field.getText();
            if (date != null) {
                field.set(time_format.parse(date));
            } else {
                field.set(null);
            }
        } catch (ParseException pe) {}
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public void actionPerformed(ActionEvent e) {
        String sdate = text_field.getText();
        Date date = null;
        if (sdate != null && !sdate.equals("")) {
            try {
                date = time_format.parse(sdate);
            } catch (ParseException pe) {
                Log.debug.out( pe);
            }
        }
        date = JCalendar.showDateDialogue(null, field.getDescription(), date);
        if (date != null) {
            text_field.setValue(date);
        }
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindTimeInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
}

